package com.wx.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wx.dao.AuthorizationMapperDao;
import com.wx.dao.SysMenuMapper;
import com.wx.dao.SysRoleMapper;
import com.wx.entity.SysMenu;
import com.wx.entity.SysMenuExample;
import com.wx.entity.SysRole;
import com.wx.entity.SysRoleExample;
import com.wx.service.AuthorizationService;

@Service
public class AuthorizationServiceImpl implements AuthorizationService {
	
	@Resource
	private AuthorizationMapperDao authorizationMapperDao;
	
	@Resource
	private SysRoleMapper sysRoleMapper;
	
	@Resource
	private SysMenuMapper sysMenuMapper;
	
	/* (non-Javadoc)
	 * @see com.wx.service.AuthorizationService#findRolesByUserId(java.lang.Integer)
	 */
	@Override
	public List<SysRole> findRolesByUserId(Integer userId) {
		List<Integer> ridl = authorizationMapperDao.getRoleIdByUserId(userId);
		if(ridl == null || ridl.isEmpty()){
			return null;
		}
		SysRoleExample example2 = new SysRoleExample();
		example2.createCriteria().andIdIn(ridl);
		List<SysRole> rl = sysRoleMapper.selectByExample(example2);
		if(rl==null || rl.isEmpty()){
			return null;
		}
		return rl;
	}

	/* (non-Javadoc)
	 * @see com.wx.service.AuthorizationService#findMenusByUserId(java.lang.Integer)
	 */
	@Override
	public List<SysMenu> findMenusByUserId(Integer userId) {
		List<Integer> menuIdis = new ArrayList<Integer>();
		List<Integer> menuId1s = authorizationMapperDao.getMenusByUserId(userId);
		if(menuId1s!=null && !menuId1s.isEmpty()){
			menuIdis.addAll(menuId1s);
		}
		
		SysMenuExample example = new SysMenuExample();
		example.createCriteria().andIdIn(menuIdis);
		 List<SysMenu> res =sysMenuMapper.selectByExample(example);
		if(res == null || res.isEmpty()){
			return null;
		}
		return res;
	}
	
	@Override
	public List<SysMenu> findAllMenusByUserId(Integer userId) {
		List<Integer> menuIdis = new ArrayList<Integer>();
		List<Integer> menuId1s = authorizationMapperDao.getMenusByUserId(userId);
		if(menuId1s!=null && !menuId1s.isEmpty()){
			menuIdis.addAll(menuId1s);
		}
		List<Integer> menuId2s = authorizationMapperDao.getMenusByUserRoles(userId);
		if(menuId2s!=null && !menuId2s.isEmpty()){
			menuIdis.addAll(menuId2s);
		}
		
		if(menuIdis == null || menuIdis.isEmpty()){
			return null;
		}
		
		SysMenuExample example = new SysMenuExample();
		example.createCriteria().andIdIn(menuIdis);
		 List<SysMenu> res =sysMenuMapper.selectByExample(example);
		if(res == null || res.isEmpty()){
			return null;
		}
		return res;
	}
	
	/* (non-Javadoc)
	 * @see com.wx.service.AuthorizationService#findMenusByRoleIds(java.lang.Integer)
	 */
	@Override
	public List<SysMenu> findMenusByRoleIds(Integer userId) { 
		List<Integer> menuIdis = new ArrayList<Integer>();
		List<Integer> menuId2s = authorizationMapperDao.getMenusByUserRoles(userId);
		if(menuId2s!=null && !menuId2s.isEmpty()){
			menuIdis.addAll(menuId2s);
		}
		
		if(menuIdis == null || menuIdis.isEmpty()){
			return null;
		}
		
		SysMenuExample example = new SysMenuExample();
		example.createCriteria().andIdIn(menuIdis);
		 List<SysMenu> res =sysMenuMapper.selectByExample(example);
		if(res == null || res.isEmpty()){
			return null;
		}
		return res;
	}

	@Override
	public List<SysMenu> findAllMenus(){
		return sysMenuMapper.selectByExample(new SysMenuExample());
	}
}
